package topcoder;

public class BallsSeparating {

	public int minOperations(int[] R, int[] G, int[] B) {
		if (R.length < 3) return -1;
	
		int minc = Integer.MAX_VALUE;
		int ci, cj, ck;
		for (int i = 0; i < 3; i++) {
			if (i == 0)
				ci = G[0] + B[0];
			else if (i == 1)
				ci = R[0] + B[0];
			else
				ci = R[0] + G[0];
			
			for (int j = 0; j < 3; j++) {
				if (j == i) continue;
				
				if (j == 0)
					cj = G[1] + B[1];
				else if (i == 1)
					cj = R[1] + B[1];
				else
					cj = R[1] + G[1];
				
				for (int k = 0; k < 3; k++) {
					if (k == i || k == j) continue;
					
					if (k == 0)
						ck = G[2] + B[2];
					else if (i == 1)
						ck = R[2] + B[2];
					else
						ck = R[2] + G[2];
					
					minc = Math.min(minc, ci + cj + ck);
				}
			}
		}
		
		for (int i = 3; i < R.length; i++) {
			minc += Math.min(G[i] + B[i], Math.min(R[i] + B[i], R[i] + G[i]));
		}
		
		return minc;
	}
	
}
